@inherits RenderViewPage
@using System.Web.Mvc.Html;
@using Umbraco.Cms.Web;
@using System.Xml;
@using System;
@using Umbraco.Framework.Dynamics;
@using PBDesk.com.Extension.UmbHelper;
@{
    Layout = "_LayoutXml.cshtml";
    var home = MyHelper.GetHomeNode(DynamicModel);
    var all = home.DescendantsOrSelf.Where("UmbracoNaviHide == @0", "False");
}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.sitemaps.org/schemas/sitemap-image/1.1"
        xmlns:video="http://www.sitemaps.org/schemas/sitemap-video/1.1">
        @foreach (var node in all)
    	  { @RenderNode(node) }
</urlset>
@helper RenderNode(BendyObject cNode)
    {
    if (cNode != null && cNode.AsDynamic().UmbracoNaviHide == "False")
    { 
     var url = cNode.AsDynamic().Url;
     var lastmod = cNode.AsDynamic().UtcModified.ToString("yyyy-MM-ddThh:mm:ss+00:00");
     var changefreq = cNode.AsDynamic().seoChangeFreq;
     var priority = cNode.AsDynamic().seoPriority;
    <url>
      @Render_loc(url)
      @Render_lastmod(changefreq ?? String.Empty,cNode.AsDynamic().UtcModified)
      @if(!string.IsNullOrEmpty(changefreq))
      {
      <changefreq>@changefreq</changefreq>
      }
      @if(!string.IsNullOrEmpty(priority))
      {
      <priority>@priority</priority>
      }
    </url>
    }
}
@helper Render_loc(string url)
    { 
        var cononicalUrl = String.Empty;
        if (Request.ServerVariables["HTTP_HOST"].ToLower().StartsWith("www"))
        { cononicalUrl = string.Concat("http://", Request.ServerVariables["HTTP_HOST"], url); }
        else
        { cononicalUrl = string.Concat("http://www.", Request.ServerVariables["HTTP_HOST"], url); }
    <loc>@cononicalUrl</loc>
}
@helper Render_lastmod(string frequercy, DateTimeOffset dtOffset)
    {
    DateTime current = DateTime.Now;
    DateTime toUse = DateTime.Now;
    DateTime fromUmb = dtOffset.DateTime;
    Random rnd = new Random();
    switch (frequercy)
    {

        case "always":
        case "hourly":
            {
                toUse = current.Subtract(new TimeSpan(1, rnd.Next(1, 59), rnd.Next(1, 59)));
                break;
            }
        case "daily":
            {
                toUse = current.Subtract(new TimeSpan(1, rnd.Next(1, 12), rnd.Next(1, 59), rnd.Next(1, 59)));
                break;
            }
        case "weekly":
            {
                toUse = current.Subtract(new TimeSpan(rnd.Next(7, 20), rnd.Next(1, 12), rnd.Next(1, 59), rnd.Next(1, 59)));
                break;
            }
        case "monthly":
            {
                toUse = current.Subtract(new TimeSpan(rnd.Next(21, 50), rnd.Next(1, 12), rnd.Next(1, 59), rnd.Next(1, 59)));
                break;
            }
        case "yearly":
        case "never":
        default:
            {
                toUse = fromUmb;
                break;
            }
    }
    DateTimeOffset fnl = new DateTimeOffset(toUse > fromUmb ? toUse : fromUmb);
    var lastmod = fnl.ToString("yyyy-MM-ddThh:mm:ss+00:00");
    <lastmod>@lastmod</lastmod>  
}